package com.lin.controller;

import com.github.pagehelper.Page;
import com.lin.domain.HouseDoor;
import com.lin.domain.HouseDoorInfo;
import com.lin.domain.HouseRepairLog;
import com.lin.domain.ProjectInfo;
import com.lin.service.PsHouseService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class HouseController {

    private Logger logger = LoggerFactory.getLogger(getClass());
    public final static String IDENTITY = "IDENTITY";


    public final static String INVOICENO = "INVOICENO";

    //session 失效时间.
    public final static Integer TIMEOUT = 300;//单位为秒
    @Autowired
    private PsHouseService houseManager;

    @RequestMapping("/")
    public ModelAndView index() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/house/authentication");
        return modelAndView;
    }


    @RequestMapping("/login")
    public String login(HttpServletRequest request) {
        logger.info("请登录 .");
        String contextPath = request.getContextPath();
        HttpSession session = request.getSession();
        Boolean flag = false;
        if (session.getAttribute(HouseController.INVOICENO) == null) {
            String invoiceNo = request.getParameter("invoiceNo");
            if (invoiceNo != null && !"".equals(invoiceNo)) {
                session.setAttribute(HouseController.INVOICENO, invoiceNo);
                session.setMaxInactiveInterval(TIMEOUT);//单位为秒
                flag = true;
            }
        }
        if (session.getAttribute(HouseController.IDENTITY) == null) {
            String papersCode = request.getParameter("papersCode");
            if (papersCode != null && !"".equals(papersCode)) {
                session.setAttribute(HouseController.IDENTITY, papersCode);
                session.setMaxInactiveInterval(TIMEOUT);//单位为秒
                flag = true;
            }
        }
        if (flag) {
            return "redirect:get_house_by_paperscode";
        }
        return "redirect:" + contextPath + "/";
    }


    @RequestMapping(value = "/get_house_by_paperscode", method = RequestMethod.GET)
    public ModelAndView getHouseByPaperCode(HttpServletRequest request, @RequestParam(required = false, defaultValue = "1") Integer page_num, @RequestParam(required = false, defaultValue = "6") Integer page_size
    ) {
        ModelAndView modelAndView = new ModelAndView();
        HttpSession session = request.getSession();
        String papersCode = (String) session.getAttribute(IDENTITY);
        String invoiceNo = (String) session.getAttribute(INVOICENO);
            Page<HouseDoor> houseDoorPage = houseManager.getHouseByPapersCodePage(papersCode, invoiceNo, page_num, page_size);
        com.github.pagehelper.PageInfo pageInfo = new com.github.pagehelper.PageInfo(houseDoorPage);
        modelAndView.addObject("page", pageInfo);
        modelAndView.addObject("papersCode", papersCode);
        modelAndView.addObject("invoiceNo", invoiceNo);
        List<ProjectInfo> projectList = houseManager.getProjectList();
        modelAndView.addObject("projectList", projectList);
        modelAndView.setViewName("/house/success");
        return modelAndView;
    }

    @RequestMapping(value = "/get_house_detail_by_id", method = RequestMethod.GET)
    public ModelAndView getHouseDetailById(@RequestParam String houseId) {
        ModelAndView modelAndView = new ModelAndView();
        HouseDoorInfo houseDoorInfo = houseManager.getHouseInfoByHouseId(houseId);
        modelAndView.addObject("houseDoorInfo", houseDoorInfo);
        modelAndView.setViewName("/house/detail");
        return modelAndView;

    }

    @RequestMapping(value = "/get_house_repairlog_list_by_id", method = RequestMethod.GET)
    public ModelAndView getHouseRepairLogListByHouseId(@RequestParam(required = false) String houseId, @RequestParam Integer page_num, @RequestParam(required = false, defaultValue = "6") Integer page_size) {
        ModelAndView modelAndView = new ModelAndView();
//        List<HouseRepairLog> repairLogList = houseManager.getHouseRepairLogListByHouseId(houseId);
//        modelAndView.addObject("repairLogList",repairLogList);
//        modelAndView.setViewName("house_repairlist");
//        return modelAndView;
        Page<HouseRepairLog> houseRepairLogPage = houseManager.getHouseRepairLogListPageByHouseId(houseId, page_num, page_size);
        com.github.pagehelper.PageInfo pageInfo = new com.github.pagehelper.PageInfo(houseRepairLogPage);
        modelAndView.addObject("page", pageInfo);
        modelAndView.addObject("houseId", houseId);
        modelAndView.setViewName("/house/record");
        return modelAndView;

    }

    @RequestMapping(value = "/repair", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> repair(@RequestParam Long houseId) {
        ModelAndView modelAndView = new ModelAndView();
//        List<Map<String, Object>> projectList = houseManager.getProjectList();
//        Map<String,Object> data = new HashMap<>();
//        Map<String,String> map = new HashMap<>();
//        for(Map<String,Object> m:projectList){
//            Set<String> keySet = m.keySet();
//            String value=null;
//            String _key = null;
//            for (String key:keySet){
//                if(key.equals("INFO_REMARK")){
//                    value = String.valueOf(m.get(key));
//                }
//                if(key.equals("INFO_ID")){
//                    _key =  String.valueOf(m.get(key));
//                }
//            }
//            map.put(_key,value);
//        }
//        modelAndView.addObject("projectList",map);
//        modelAndView.addObject("houseId",houseId);
//        modelAndView.setViewName("house_repair");
//        return modelAndView;

//        data.put("success",true);
//        data.put("list",map);
//        return data;
        List<ProjectInfo> projectList = houseManager.getProjectList();
        return null;

    }

    @RequestMapping(value = "/save_repair", method = RequestMethod.POST)
    @ResponseBody
    public String saveRepair(@RequestParam Integer infoId,
                             @RequestParam String tel,
                             @RequestParam String houseId,
                             @RequestParam String remark) {
            HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("houseId", houseId);
        map.put("tel", tel);
        map.put("infoId", infoId);
        map.put("createDate", new Date());
        map.put("remark", remark);
        houseManager.saveRepairUser(map);
        Object id = map.get("id");
        System.out.println(id);
        return "success";

    }

    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public void logout(HttpServletRequest request, HttpServletResponse response) throws IOException {
        HttpSession session = request.getSession();
        session.removeAttribute(IDENTITY);
        session.removeAttribute(INVOICENO);
        session.invalidate();
        String contextPath = request.getContextPath();
        response.sendRedirect(contextPath + "/");
    }
}
